package com.internal.Login.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class LoginClientUtil {
	
	private final LoginContent loginContent ;
	public LoginClientUtil(LoginContent loginContent){
		this.loginContent = loginContent;
	}

	public void login(String userId, String pw) {
		LoginService.Util.getInstance().login(userId, pw,
				new AsyncCallback<String>() {
					@Override
					public void onSuccess(String result) {
						if (result.equalsIgnoreCase("fail")) {
							Window.alert("يجب التأكد من اسم المستـخدم او كلمه السر");
						} else if (result.equalsIgnoreCase("noactive")) {
							Window.alert("يجب تفعيل المستخدم اولا");
						} else if (result.equalsIgnoreCase("success")) {
							Window.Location.assign(GWT.getHostPageBaseURL()
									+ "Registration.html?action=update");
						}
					}

					@Override
					public void onFailure(Throwable caught) {
						Window.alert("يجب التأكد من اسم المستـخدم او كلمه السر");

					}
				});
	}

	public DialogBox showForgetPWDial(String dialogTitle, String labelTitle) {
		final DialogBox textDialogBox = new DialogBox();

		VerticalPanel dialogContents = new VerticalPanel();
		dialogContents.setSpacing(4);
		dialogContents.setTitle(dialogTitle);
		textDialogBox.setWidget(dialogContents);

		HTML title = new HTML(labelTitle);
		dialogContents.add(title);
		dialogContents.setCellHorizontalAlignment(title,
				HasHorizontalAlignment.ALIGN_RIGHT);

		final TextBox userIdTxt = new TextBox();
		userIdTxt.setWidth("250px");
		dialogContents.add(userIdTxt);
		dialogContents.setCellHorizontalAlignment(userIdTxt,
				HasHorizontalAlignment.ALIGN_CENTER);

		HorizontalPanel btnPanel = new HorizontalPanel();
		btnPanel.setSpacing(10);

		Button close = new Button("اغلاق");
		close.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				textDialogBox.hide();
			}
		});

		Button sendPW = new Button("ارسال كلمة السر");
		sendPW.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				sendPassowrd(userIdTxt.getText());
				textDialogBox.hide();
			}
		});

		btnPanel.add(sendPW);
		btnPanel.add(close);
		dialogContents.add(btnPanel);
		dialogContents.setCellHorizontalAlignment(btnPanel,
				HasHorizontalAlignment.ALIGN_CENTER);

		return textDialogBox;
	}

	protected void sendPassowrd(String id) {
		LoginService.Util.getInstance().sendPassword(id,
				new AsyncCallback<Void>() {

					@Override
					public void onFailure(Throwable caught) {
						Window.alert(" فشا ارسال كلمة السر .. حـــــاول لاحقا ");
					}

					@Override
					public void onSuccess(Void result) {
						// make it return boolean to indicate success
						Window.alert("تم إرســـال كلمة السر ");
					}
				});

	}

}
